//5185. 存在连续三个奇数的数组

/**
 * @param {number[]} arr
 * @return {boolean}
 */
var threeConsecutiveOdds = function(arr) {
    let nOdd =0
    for(let i=0;i<arr.length;i++){
        nOdd = (arr[i]&1)?nOdd+1:0
        if(nOdd===3) return true
    }
    return false
};

threeConsecutiveOdds([1,2,3])

//5488. 使数组中所有元素相等的最小操作数

/**
 * @param {number} n
 * @return {number}
 */
var minOperations = function(n) {
    let op = 0
    let mid = Math.floor(n/2)
    if(n&1){//技数
        op = mid*(2+(mid*2))/2
    }else{
        op = mid*(1+(mid*2-1))/2
    }
    return op
};

minOperations(3)

//5489. 两球之间的磁力
/**
 * @param {number[]} position
 * @param {number} m
 * @return {number}
 */
var maxDistance = function(position, m) {
    position.sort((a,b)=>{
        return a - b
    })
    let l = 1
    let h = position[position.length-1] - position[0]
    let mid = 0
    let succ = 1
    while(l<=h){
         mid = Math.floor((l+h)/2)
        if(determine(position,m,mid)){
            l = mid + 1
            succ = mid
        }else{
            h =  mid - 1
        }
    }
    return succ
};

let determine=(arr,n,space)=>{
    let num = 1
    let last = arr[0]
    for(let i=1;i<arr.length;i++){
        if(arr[i]-last>=space) {
            num++
            last = arr[i]
        }
        if(num===n) return true
    }
    return false
}

let ans =maxDistance(
    [5,4,3,2,1,1000000000], 2
)
console.log(ans)


//5490. 吃掉 N 个橘子的最少天数
/**
 * @param {number} n
 * @return {number}
 */

 ///dp内存空间不足
// var minDays = function(n) {
//     let dp = new Array(n+1)
//     dp[0]=0
//     dp[1]=1
//     for(let i=2;i<=n;i++){
//         let tmp = Number.MAX_VALUE
//         if(i&1===0){
//             tmp = dp[i>>2]
//         }
//         if(i%3===0){
//             tmp = Math.min(tmp,dp[i-(2*(i/3))])
//         }
//         dp[i]= Math.min(tmp,dp[i-1])+1
//     }
//     return dp[n]
// };

var minDays = n=>{
    if(n===0) return 0
    if(n===1) return 1
    return 1+Math.min(minDays(Math.floor(n/2))+n%2,minDays(Math.floor(n/3))+n%3) 
}

minDays(3)